package com.itheima.filter;


import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Configuration
public class AuthTokenFilter implements GlobalFilter, Ordered {
    private final static Log log = LogFactory.getLog(AuthTokenFilter.class);
    private static final String TOKEN = "token";
    /*
     * @Description: 小----->高
     * @Method: getOrder
     * @Param: []
     * @Update:
     * @since: 1.0.0
     * @Return: int
     *
     */
    @Override
    public int getOrder() {
        return 0;
    }


    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        String token = exchange.getRequest().getQueryParams().getFirst(TOKEN);
        if (StringUtils.isEmpty(token)) {
            log.error("非法访问>>>>>>>>");
            exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
            return exchange.getResponse().setComplete();

        }
        return chain.filter(exchange);


    }

}